#include "mainwindow.h"
#include "ui_mainwindow.h"
HYPER_PARAMETER hyper_parameter;
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //serial
    connect(&mSerial,SIGNAL(readyRead()),this,SLOT(on_uart_data_recive()));
    ui_default_value_init();

    on_serversimuart_BtnRefresh_clicked();
    mCycle_buf.head = 0;
    mCycle_buf.tail = 0;

    //timer
    mTimer.setInterval(100);
    connect(&mTimer,SIGNAL(timeout()),this,SLOT(on_timer()));
    mTimer.start(100);

    //禁止输入
    input_restrict();

    //输入限制
    input_restrict_len();


}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::ui_default_value_init()
{
    ui->cmb_baud->setCurrentText("115200");
    ui->cbb_administrative_region->setCurrentText("宁夏回族自治区");
    ui->cbb_managemen_agency->setCurrentText("西干渠管理处");
    ui->cbb_station_yard->setCurrentText("一所");
    ui->cbb_device_type->setCurrentText("测控一体化闸门");
    ui->cbb_device_vender->setCurrentText("陕西兴源");
}


void MainWindow::input_restrict()
{
    ui->edit_recv_show->setReadOnly(true);
}
void MainWindow::input_restrict_len()
{
    QRegExp regExp1("[0-9]{1,5}");
    QRegExp regExp2("[0-9]{1,3}");

    QRegExpValidator *pRegExpValidator1 = new QRegExpValidator(regExp1,this);
    QRegExpValidator *pRegExpValidator2 = new QRegExpValidator(regExp2,this);

    ui->stake_mark_front -> setValidator(pRegExpValidator1);
    ui->stake_mark_back  -> setValidator(pRegExpValidator1);
    ui->serial_number    -> setValidator(pRegExpValidator2);

}

void MainWindow::on_timer()
{
    ui->sys_time->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));

    //处理串口数据
    if( flag_uart_recive )
    {
        uart_data_handle();
        flag_uart_recive = false;
    }

}


void MainWindow::uart_data_handle()
{
    unsigned short pkglen = 0;
    static unsigned char workstep = 0;
    //如果队列数据为空 返回
    if( mCycle_buf.head == mCycle_buf.tail )
    {
        return;
    }
//    if( flag_set_parameter_mode == false )
//    {
        if( (mCycle_buf.data_buf[mCycle_buf.tail] != 0xAA) || ( mCycle_buf.data_buf[(mCycle_buf.tail+1)%MAX_RECV_BUF_SIZE] != 0x55 ) )
        {
            QString recv_data = "";
            recv_data += QDateTime::currentDateTime().toString("[yyyy-MM-dd hh:mm:ss]:");
            QByteArray byteArray(reinterpret_cast<const char*>(mCycle_buf.data_buf), mCycle_buf.size);
            recv_data += QString::fromUtf8(byteArray);

            mCycle_buf.head = 0;
            mCycle_buf.tail = 0;
            mCycle_buf.size = 0;
            ui ->edit_recv_show ->append(recv_data);
        }
//    }

    if( workstep == 0 )
    {
        //校验帧头
        do{
            if( (mCycle_buf.data_buf[mCycle_buf.tail] == 0xAA) && ( mCycle_buf.data_buf[(mCycle_buf.tail+1)%MAX_RECV_BUF_SIZE] == 0x55 ) )
            {
                workstep = 1;
                break;
            }
            else
            {
                mCycle_buf.tail++;
                mCycle_buf.tail %= MAX_RECV_BUF_SIZE;
            }
        }while( mCycle_buf.tail != mCycle_buf.head );
    }

    // package length
    if( workstep == 1 )
    {
        pkglen = mCycle_buf.data_buf[(mCycle_buf.tail+2)%MAX_RECV_BUF_SIZE];
        pkglen = pkglen * 256 + mCycle_buf.data_buf[(mCycle_buf.tail+3)%MAX_RECV_BUF_SIZE];
    }

    if( pkglen > 500 || pkglen == 0)
    {

        mCycle_buf.tail++;
        mCycle_buf.tail %= MAX_RECV_BUF_SIZE;
        workstep = 0;
        return;
    }

    if( mCycle_buf.head > mCycle_buf.tail )
    {
        if( (mCycle_buf.head - mCycle_buf.tail) < pkglen )
        {
            return;
        }
    }
    else if(mCycle_buf.head < mCycle_buf.tail)
    {
        if( ( (MAX_RECV_BUF_SIZE + mCycle_buf.head) - mCycle_buf.tail) < pkglen )
        {
            return;
        }
    }
    unsigned short sumcrc = 0;
    unsigned short reccrc = 0;

    //校验位判断
    for( int i = 0; i < pkglen - 2; i++ )
    {
        sumcrc += mCycle_buf.data_buf[(mCycle_buf.tail + i)%MAX_RECV_BUF_SIZE];
    }

    reccrc = mCycle_buf.data_buf[(mCycle_buf.tail + pkglen - 2)%MAX_RECV_BUF_SIZE];
    reccrc = reccrc * 256 + mCycle_buf.data_buf[(mCycle_buf.tail + pkglen - 1)%MAX_RECV_BUF_SIZE];
    if( sumcrc != reccrc )
    {
        mCycle_buf.tail++;
        mCycle_buf.tail %= MAX_RECV_BUF_SIZE;
        workstep = 0;
        return;
    }

    // copy data to uart_recv_buf
    memset( uart_recv_buf, 0, sizeof(uart_recv_buf));
    if( mCycle_buf.tail + pkglen < MAX_RECV_BUF_SIZE )
    {
        memcpy( uart_recv_buf, mCycle_buf.data_buf + mCycle_buf.tail, pkglen );
    }
    else if( mCycle_buf.tail + pkglen >= MAX_RECV_BUF_SIZE )
    {
        memcpy( uart_recv_buf, mCycle_buf.data_buf + mCycle_buf.tail, MAX_RECV_BUF_SIZE - mCycle_buf.tail );
        memcpy( uart_recv_buf + (MAX_RECV_BUF_SIZE - mCycle_buf.tail),  mCycle_buf.data_buf, ( pkglen - ( MAX_RECV_BUF_SIZE - mCycle_buf.tail ) ) );
    }
    mCycle_buf.tail += pkglen;
    mCycle_buf.tail %= MAX_RECV_BUF_SIZE;
    if( flag_uart_open == false )
    {
        return ;
    }
    if( uart_recv_buf[4] == 0x01 )
    {
        recv_hyper_parameter_to_show();
    }
    else if( uart_recv_buf[4] == 0x02 )
    {
        QString str = "设置的设备地址冲突。请检查选择项，重新设置！";
        QMessageBox* message = new QMessageBox(QMessageBox::Warning, "提示信息", str, QMessageBox::Ok);
        message->setAttribute(Qt::WA_DeleteOnClose); //关闭时删除对象
        message->setModal(false);
        message->show();
    }

    //在上方显示框添加显示数据
    uart_data_edit_show( uart_recv_buf, pkglen );
}
void MainWindow::hex_to_str(const uint8_t *data, int len, char *output) {
    const char hex_chars[] = "0123456789ABCDEF";
    for (int i = 0; i < len; i++) {
        output[2*i]   = hex_chars[(data[i] >> 4) & 0x0F]; // 处理高四位
        output[2*i+1] = hex_chars[data[i] & 0x0F];       // 处理低四位
    }
    output[2*len] = '\0'; // 添加字符串结束符
}
void MainWindow::recv_hyper_parameter_to_show()
{
    memcpy(&hyper_parameter, uart_recv_buf+5, sizeof(hyper_parameter));
	switch( (hyper_parameter.addr.region) )
    {
        case 0x0B:  ui->cbb_administrative_region->setCurrentIndex(0);   	break;
        case 0x0C:  ui->cbb_administrative_region->setCurrentIndex(1);   	break;
        case 0x0D:  ui->cbb_administrative_region->setCurrentIndex(2);   	break;
        case 0x0E:  ui->cbb_administrative_region->setCurrentIndex(3);   	break;
        case 0x0F:  ui->cbb_administrative_region->setCurrentIndex(4);   	break;
        case 0x15:  ui->cbb_administrative_region->setCurrentIndex(5);   	break;
        case 0x16:  ui->cbb_administrative_region->setCurrentIndex(6);   	break;
        case 0x17:  ui->cbb_administrative_region->setCurrentIndex(7);   	break;
        case 0x1F:  ui->cbb_administrative_region->setCurrentIndex(8);   	break;
        case 0x20:  ui->cbb_administrative_region->setCurrentIndex(9);  	break;
        case 0x21:  ui->cbb_administrative_region->setCurrentIndex(10);  	break;
        case 0x22:  ui->cbb_administrative_region->setCurrentIndex(11);  	break;
        case 0x23:  ui->cbb_administrative_region->setCurrentIndex(12);  	break;
        case 0x24:  ui->cbb_administrative_region->setCurrentIndex(13);  	break;
        case 0x25:  ui->cbb_administrative_region->setCurrentIndex(14);  	break;
        case 0x29:  ui->cbb_administrative_region->setCurrentIndex(15);  	break;
        case 0x2A:  ui->cbb_administrative_region->setCurrentIndex(16);  	break;
        case 0x2B:  ui->cbb_administrative_region->setCurrentIndex(17);  	break;
        case 0x2C:  ui->cbb_administrative_region->setCurrentIndex(18);  	break;
        case 0x2D:  ui->cbb_administrative_region->setCurrentIndex(19);  	break;
        case 0x2E:  ui->cbb_administrative_region->setCurrentIndex(20);  	break;
        case 0x32:  ui->cbb_administrative_region->setCurrentIndex(21);  	break;
        case 0x33:  ui->cbb_administrative_region->setCurrentIndex(22);  	break;
        case 0x34:  ui->cbb_administrative_region->setCurrentIndex(23);  	break;
        case 0x35:  ui->cbb_administrative_region->setCurrentIndex(24);  	break;
        case 0x36:  ui->cbb_administrative_region->setCurrentIndex(25);  	break;
        case 0x3D:  ui->cbb_administrative_region->setCurrentIndex(26);  	break;
        case 0x3E:  ui->cbb_administrative_region->setCurrentIndex(27);  	break;
        case 0x3F:  ui->cbb_administrative_region->setCurrentIndex(28);  	break;
        case 0x40:  ui->cbb_administrative_region->setCurrentIndex(29);  	break;
        case 0x41:  ui->cbb_administrative_region->setCurrentIndex(30);  	break;
        default :   ui->cbb_administrative_region->setCurrentIndex(-1);     break;
    }
    ui->cbb_managemen_agency->setCurrentIndex( hyper_parameter.addr.management-1 );
    ui->cbb_station_yard->setCurrentIndex( hyper_parameter.addr.station-1 );
    ui->cbb_device_type->setCurrentIndex( hyper_parameter.addr.dev_type-1 );
    ui->cbb_device_vender->setCurrentIndex( hyper_parameter.addr.dev_vender-1 );
    ui->stake_mark_front->setText( QString::number( hyper_parameter.addr.stake_front ) );
    ui->stake_mark_back->setText( QString::number( hyper_parameter.addr.stake_back ) );
    ui->serial_number->setText( QString::number( hyper_parameter.addr.serial ) );

    char str[10];
    hex_to_str((uint8_t *)&hyper_parameter.addr, 10, str );
    ui->device_number->setText( str );

    ui->HW_version->setText( QString::number( hyper_parameter.hardware_version ) );
    ui->SW_version->setText( QString::number( hyper_parameter.software_version ) );

//    ui->lineEdit_4 ->setText( QString::number( hyper_parameter.encoder_upper_limit ) );
//    ui->lineEdit_5 ->setText( QString::number( hyper_parameter.encoder_lower_limit ) );
//    ui->lineEdit_6 ->setText( QString::number( hyper_parameter.gate_front_water_stage_upper_limit ) );
//    ui->lineEdit_7 ->setText( QString::number( hyper_parameter.gate_front_water_stage_lower_limit ) );
//    ui->lineEdit_8 ->setText( QString::number( hyper_parameter.gate_back_water_stage_upper_limit ) );
//    ui->lineEdit_9 ->setText( QString::number( hyper_parameter.gate_back_water_stage_lower_limit ) );
//    ui->lineEdit_10->setText( QString::number( hyper_parameter.voltage_upper_limit ) );
//    ui->lineEdit_11->setText( QString::number( hyper_parameter.voltage_lower_limit ) );
//    ui->lineEdit_12->setText( QString::number( hyper_parameter.current_upper_limit ) );
//    ui->lineEdit_13->setText( QString::number( hyper_parameter.current_lower_limit ) );
//    ui->lineEdit_14->setText( QString::number( hyper_parameter.temperature_upper_limit ) );
//    ui->lineEdit_15->setText( QString::number( hyper_parameter.temperature_lower_limit ) );
//    ui->lineEdit_16->setText( QString::number( hyper_parameter.gate_front_water_stage_addr ) );
//    ui->lineEdit_17->setText( QString::number( hyper_parameter.gate_back_water_stage_addr ) );
//    ui->lineEdit_18->setText( QString::number( hyper_parameter.gate_front_water_stage_zero ) );
//    ui->lineEdit_19->setText( QString::number( hyper_parameter.gate_back_water_stage_zero ) );
//    ui->lineEdit_20->setText( QString::number( hyper_parameter.gate_height ) );
//    ui->lineEdit_21->setText( QString::number( hyper_parameter.gate_width ) );
//    ui->lineEdit_22->setText( QString::number( hyper_parameter.gate_full_open_time ) );

//    sprintf( str, "%u.%u.%u.%u", hyper_parameter.upload_target_server_ip[0], hyper_parameter.upload_target_server_ip[1], hyper_parameter.upload_target_server_ip[2], hyper_parameter.upload_target_server_ip[3] );
//    ui->lineEdit_23->setText( str );

//    ui->lineEdit_24->setText( QString::number( hyper_parameter.upload_target_port ) );
//    ui->lineEdit_25->setText( QString::number( hyper_parameter.reconnect_server_timer ) );
//    ui->updata_time->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
}
void MainWindow::uart_data_edit_show( unsigned char * data, uint len )
{
    //在上方显示框添加显示数据
    QString recv_data = "";
    recv_data += QDateTime::currentDateTime().toString("[yyyy-MM-dd hh:mm:ss]:");
    for( uint i = 0; i < len; i++ )
    {
        recv_data += QString(" %1").arg(data[i], 2, 16, QLatin1Char('0'));
    }
    ui ->edit_recv_show ->append(recv_data);
}


void MainWindow::on_uart_data_recive()
{
    QByteArray recvData = mSerial.readAll();

    unsigned char *data = reinterpret_cast<unsigned char*>(recvData.data());
    int datasize = recvData.size();

    //接收到数据，将数据插入队列
    if( mCycle_buf.head + datasize < MAX_RECV_BUF_SIZE )
    {
        memcpy( mCycle_buf.data_buf + mCycle_buf.head, data, datasize );
        mCycle_buf.size += datasize;
    }
    else if( mCycle_buf.head + datasize >= MAX_RECV_BUF_SIZE )
    {
        memcpy( mCycle_buf.data_buf + mCycle_buf.head, data, MAX_RECV_BUF_SIZE - mCycle_buf.head );
        memcpy( mCycle_buf.data_buf, mCycle_buf.data_buf + (MAX_RECV_BUF_SIZE - mCycle_buf.head), ( datasize - ( MAX_RECV_BUF_SIZE - mCycle_buf.head ) ) );
        mCycle_buf.size += datasize;
    }

    mCycle_buf.head += datasize;
    mCycle_buf.head %= MAX_RECV_BUF_SIZE;

    flag_uart_recive = true;
}

void MainWindow::on_btn_open_clicked()
{
    bool ok;
    QString portName = ui->serversimuart_CBoxSerialPort->currentText();
    //打开串口
    if( mSerial.isOpen() == true )
    {
        mSerial.close();
        ui->btn_open->setText("打开串口");
    }
    else
    {
        if( portName.size() == 0 )
        {
            on_serversimuart_BtnRefresh_clicked();
            if( portName.size() == 0)
            {
                return ;
            }
        }

        if(portName.at(4) == ' ')
        {
            portName = portName.left(4);
        }
        else if(portName.at(5) == ' ')
        {
            portName = portName.left(5);
        }
        int portNumber = portName.mid(3).toInt(&ok); // 提取数字部分
        if (ok && portNumber > 9)
        {
            portName = "\\\\.\\" + portName; // 转换为\\.\COM10格式
        }

        mSerial.setPortName( portName );  //设置串口名字


        mSerial.setBaudRate(ui->cmb_baud->currentText().toUInt());

        mSerial.setReadBufferSize(30);

        //校验位（无校验）
        mSerial.setParity(QSerialPort::NoParity);

        //数据位（8）
        mSerial.setDataBits(QSerialPort::Data8);

        //停止位（1）
        mSerial.setStopBits(QSerialPort::OneStop);

        if( mSerial.open(QSerialPort::ReadWrite) )
        {
            ui->edit_recv_show->append("已经打开串口"+ui->serversimuart_CBoxSerialPort->currentText());
            ui->btn_open->setText("关闭串口");
            flag_uart_open = true;
        }
    }
}


void MainWindow::on_serversimuart_BtnRefresh_clicked()
{
    ui->serversimuart_CBoxSerialPort->clear();//清空下拉列表框内容
    QList<QSerialPortInfo> serialPortInfo = QSerialPortInfo::availablePorts();
    //在count中存储serialPortInfo的大小
    int count = serialPortInfo.count();
    //遍历这个链表,把链表中的每个元素弄进下拉列表框里
    for (int i=0;i<count ;i++ )
    {
        QString str_show;
        str_show.append(serialPortInfo.at(i).portName());
        str_show.append( " " );
        str_show.append(serialPortInfo.at(i).description());
        ui->serversimuart_CBoxSerialPort->addItem( str_show );
    }
    ui->edit_recv_show->append("刷新串口");
}

//void MainWindow::on_btn_set_mode_clicked()
//{
//    flag_set_parameter_mode = !flag_set_parameter_mode;
//    if( flag_set_parameter_mode == true )
//    {
//        ui->edit_recv_show->append("切换为设置参数模式！");
//        ui->btn_set_mode->setText("监测调试日志");
//    }
//    else
//    {
//        ui->edit_recv_show->append("切换为监测调试日志模式！");
//        ui->btn_set_mode->setText("设置参数");
//    }
//}

void MainWindow::on_pushButton_2_clicked()
{
    ui->edit_recv_show->clear();
}

void MainWindow::on_pushButton_3_clicked()
{
    ui->edit_send_show->clear();
}

void MainWindow::on_device_parameter_get_clicked()
{
    unsigned char sendbuf[8];
    unsigned short sendlen = 8;
    sendbuf[0] = 0xAA;
    sendbuf[1] = 0x55;

    sendbuf[2] = sendlen >> 8;
    sendbuf[3] = sendlen;

    sendbuf[4] = 0x12;
    sendbuf[5] = 0x10;

    unsigned short sumcrc = 0;
    for( int i = 0; i < sendlen - 2; i++ )
    {
        sumcrc += sendbuf[i];
    }
    sendbuf[sendlen - 2] = ( sumcrc >> 8 ) & 0xFF;
    sendbuf[sendlen - 1] = ( sumcrc >> 0 ) & 0xFF;

    mSerial.write( reinterpret_cast <char *>(sendbuf), sendlen );
    //在上方显示框添加显示数据
    QString send_data = "";
    send_data += QDateTime::currentDateTime().toString("[yyyy-MM-dd hh:mm:ss]:");
    for( uint i = 0; i < sendlen; i++ )
    {
        send_data += QString(" %1").arg(sendbuf[i], 2, 16, QLatin1Char('0'));
    }
    ui ->edit_send_show ->append(send_data);
}

void MainWindow::on_device_parameter_set_clicked()
{
    unsigned char sendbuf[16];
    unsigned short sendlen = 0;
    unsigned short datapos = 0;
    unsigned short temp;
    memset(sendbuf, 0, 16);
    sendbuf[datapos]    = 0xAA;
    sendbuf[datapos+1]  = 0x55;
    datapos+=2;
    sendbuf[datapos]    = sendlen >> 8;
    sendbuf[datapos+1]  = sendlen;
    datapos+=2;
    // 行政区
    switch( (ui->cbb_administrative_region->currentIndex() ) )
    {
        case 0:   sendbuf[datapos] = 0x0B;   break;
        case 1:   sendbuf[datapos] = 0x0C;   break;
        case 2:   sendbuf[datapos] = 0x0D;   break;
        case 3:   sendbuf[datapos] = 0x0E;   break;
        case 4:   sendbuf[datapos] = 0x0F;   break;
        case 5:   sendbuf[datapos] = 0x15;   break;
        case 6:   sendbuf[datapos] = 0x16;   break;
        case 7:   sendbuf[datapos] = 0x17;   break;
        case 8:   sendbuf[datapos] = 0x1F;   break;
        case 9:  sendbuf[datapos] = 0x20;    break;
        case 10:  sendbuf[datapos] = 0x21;   break;
        case 11:  sendbuf[datapos] = 0x22;   break;
        case 12:  sendbuf[datapos] = 0x23;   break;
        case 13:  sendbuf[datapos] = 0x24;   break;
        case 14:  sendbuf[datapos] = 0x25;   break;
        case 15:  sendbuf[datapos] = 0x29;   break;
        case 16:  sendbuf[datapos] = 0x2A;   break;
        case 17:  sendbuf[datapos] = 0x2B;   break;
        case 18:  sendbuf[datapos] = 0x2C;   break;
        case 19:  sendbuf[datapos] = 0x2D;   break;
        case 20:  sendbuf[datapos] = 0x2E;   break;
        case 21:  sendbuf[datapos] = 0x32;   break;
        case 22:  sendbuf[datapos] = 0x33;   break;
        case 23:  sendbuf[datapos] = 0x34;   break;
        case 24:  sendbuf[datapos] = 0x35;   break;
        case 25:  sendbuf[datapos] = 0x36;   break;
        case 26:  sendbuf[datapos] = 0x3D;   break;
        case 27:  sendbuf[datapos] = 0x3E;   break;
        case 28:  sendbuf[datapos] = 0x3F;   break;
        case 29:  sendbuf[datapos] = 0x40;   break;
        case 30:  sendbuf[datapos] = 0x41;   break;
        default :                            break;
    }
    datapos+=1;

    // 管理处
    sendbuf[datapos] = ui->cbb_managemen_agency->currentIndex() + 1;
    datapos+=1;

    // 站所
    sendbuf[datapos] = ui->cbb_station_yard->currentIndex() + 1;
    datapos+=1;

    // 桩号(前)
    if( ui->stake_mark_front->text().toUShort() <= 0xFFFF )
    {
        temp = ui->stake_mark_front->text().toUShort();
        sendbuf[datapos]   = temp;
        sendbuf[datapos+1] = temp >> 8;
        datapos+=2;
    }
    else
    {
        QString str = "输入数值超出范围，请输入0~65535范围以内的数值！";
        QMessageBox* message = new QMessageBox(QMessageBox::Warning, "提示信息", str, QMessageBox::Ok);
        message->setAttribute(Qt::WA_DeleteOnClose); //关闭时删除对象
        message->setModal(false);
        message->show();
        return ;
    }

    // 桩号(后)
    if( ui->stake_mark_back->text().toUShort() <= 0xFFFF )
    {
        temp = ui->stake_mark_back->text().toUShort();
        sendbuf[datapos]   = temp;
        sendbuf[datapos+1] = temp >> 8;
        datapos+=2;
    }
    else
    {
        QString str = "输入数值超出范围，请输入0~65535范围以内的数值！";
        QMessageBox* message = new QMessageBox(QMessageBox::Warning, "提示信息", str, QMessageBox::Ok);
        message->setAttribute(Qt::WA_DeleteOnClose); //关闭时删除对象
        message->setModal(false);
        message->show();
        return ;
    }

    // 设备类型
    sendbuf[datapos] = ui->cbb_device_type->currentIndex() + 1;
    datapos+=1;

    // 设备厂家
    sendbuf[datapos] = ui->cbb_device_vender->currentIndex() + 1;
    datapos+=1;

    // 顺序编号
    if( ui->serial_number->text().toUShort() <= 0xFF )
    {
        sendbuf[datapos] = ui->serial_number->text().toUShort();
        datapos+=1;
    }
    else
    {
        QString str = "输入数值超出范围，请输入0~255范围以内的数值！";
        QMessageBox* message = new QMessageBox(QMessageBox::Warning, "提示信息", str, QMessageBox::Ok);
        message->setAttribute(Qt::WA_DeleteOnClose); //关闭时删除对象
        message->setModal(false);
        message->show();
        return ;
    }

    sendlen = datapos + 2;
    sendbuf[2]  = sendlen >> 8;
    sendbuf[3]  = sendlen;

    unsigned short sumcrc = 0;
    for( int i = 0; i < sendlen - 2; i++ )
    {
        sumcrc += sendbuf[i];
    }
    sendbuf[sendlen - 2] = ( sumcrc >> 8 ) & 0xFF;
    sendbuf[sendlen - 1] = ( sumcrc >> 0 ) & 0xFF;

    mSerial.write( reinterpret_cast <char *>(sendbuf), sendlen );
    //在上方显示框添加显示数据
    QString send_data = "";
    send_data += QDateTime::currentDateTime().toString("[yyyy-MM-dd hh:mm:ss]:");
    for( uint i = 0; i < sendlen; i++ )
    {
        send_data += QString(" %1").arg(sendbuf[i], 2, 16, QLatin1Char('0'));
    }
    ui ->edit_send_show ->append(send_data);
}
